package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.bo.MallOrderDetailBo;
import com.ruoyi.mall.domain.vo.MallOrderDetailVo;
import com.ruoyi.mall.domain.MallOrderDetail;
import com.ruoyi.mall.mapper.MallOrderDetailMapper;
import com.ruoyi.mall.service.IMallOrderDetailService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 积分商城订单明细Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-20
 */
@RequiredArgsConstructor
@Service
public class MallOrderDetailServiceImpl implements IMallOrderDetailService {

    private final MallOrderDetailMapper baseMapper;

    /**
     * 查询积分商城订单明细
     */
    @Override
    public MallOrderDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询积分商城订单明细列表
     */
    @Override
    public TableDataInfo<MallOrderDetailVo> queryPageList(MallOrderDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MallOrderDetail> lqw = buildQueryWrapper(bo);
        Page<MallOrderDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询积分商城订单明细列表
     */
    @Override
    public List<MallOrderDetailVo> queryList(MallOrderDetailBo bo) {
        LambdaQueryWrapper<MallOrderDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MallOrderDetail> buildQueryWrapper(MallOrderDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallOrderDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getScoreMallOrderDetailId()), MallOrderDetail::getScoreMallOrderDetailId, bo.getScoreMallOrderDetailId());
        lqw.eq(StringUtils.isNotBlank(bo.getScoreMallOrderId()), MallOrderDetail::getScoreMallOrderId, bo.getScoreMallOrderId());
        lqw.eq(bo.getGoodsId() != null, MallOrderDetail::getGoodsId, bo.getGoodsId());
        lqw.like(StringUtils.isNotBlank(bo.getGoodsName()), MallOrderDetail::getGoodsName, bo.getGoodsName());
        lqw.eq(bo.getGoodsPayRule() != null, MallOrderDetail::getGoodsPayRule, bo.getGoodsPayRule());
        lqw.eq(bo.getGoodsScore() != null, MallOrderDetail::getGoodsScore, bo.getGoodsScore());
        lqw.eq(bo.getGoodsMoney() != null, MallOrderDetail::getGoodsMoney, bo.getGoodsMoney());
        lqw.eq(bo.getGoodsAmount() != null, MallOrderDetail::getGoodsAmount, bo.getGoodsAmount());
        lqw.eq(bo.getGoodsSpecificationsId() != null, MallOrderDetail::getGoodsSpecificationsId, bo.getGoodsSpecificationsId());
        lqw.like(StringUtils.isNotBlank(bo.getSpecificationsName()), MallOrderDetail::getSpecificationsName, bo.getSpecificationsName());
        lqw.eq(bo.getGoodsSpecificationsDetailId() != null, MallOrderDetail::getGoodsSpecificationsDetailId, bo.getGoodsSpecificationsDetailId());
        lqw.like(StringUtils.isNotBlank(bo.getSpecificationsDetailName()), MallOrderDetail::getSpecificationsDetailName, bo.getSpecificationsDetailName());
        lqw.eq(bo.getYn() != null, MallOrderDetail::getYn, bo.getYn());
        return lqw;
    }

    /**
     * 新增积分商城订单明细
     */
    @Override
    public Boolean insertByBo(MallOrderDetailBo bo) {
        MallOrderDetail add = BeanUtil.toBean(bo, MallOrderDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertBatch(List<MallOrderDetail> lstMallOrderDetails)
    {
        return baseMapper.insertBatch(lstMallOrderDetails);
    }

    /**
     * 修改积分商城订单明细
     */
    @Override
    public Boolean updateByBo(MallOrderDetailBo bo) {
        MallOrderDetail update = BeanUtil.toBean(bo, MallOrderDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallOrderDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除积分商城订单明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
